<template>
  <!-- 登录框 -->
  <div class="login-container">
    <!-- 头像 -->
    <div class="login-header">
      <img src="../imgs/big-mac.jpg" alt="" />
    </div>
    <!-- 提交表单 -->
    <div class="login-form">
      <el-form :model="loginFrom" :rules="loginRules" ref='loginRef'>
        <el-form-item prop='username'>
          <el-input placeholder="请输入用户名" v-model="loginFrom.username"></el-input>
        </el-form-item>
        <el-form-item prop='password'>
          <el-input placeholder="请输入密码" v-model="loginFrom.password" ></el-input>
        </el-form-item>
        <div class="btns">
          <el-form-item>
            <el-button type="primary" @click="submitForm">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {login} from '../api/index.js'
export default {
  data() {
    return {
      // 表单数据
      loginFrom: {
        username: '',
        password: '',
      },
      // 表单规则
      loginRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 6, message: '用户名长度在 3 到 6 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '密码长度在 3 到 10 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    // 登录
    submitForm() {
      // 
      this.$refs.loginRef.validate((valid)=>{
        if(valid){
           //向后台验证用户的账号
            login(this.loginFrom).then(res=>{
                console.log(res);
                console.log(this.loginFrom);
                if (res.data.meta.status == 400) {
                  this.$message.error(res.data.meta.msg)
                }else if (res.data.meta.status == 200) {
                  this.$message.success(res.data.meta.msg)
                  var s = res.data.data.token
                  window.sessionStorage.setItem('token',s)
                  this.$router.push('/layout')
                }
            })
        }else{
          this.$message.error('请输入完整的用户名或密码');
            return false;
        }
      })
    },

    // 重置
    resetForm() {
      this.$refs.loginRef.resetFields()
    },
  },
}
</script>

<style lang="less">
.login-container {
  width: 500px;
  height: 350px;
  background: aliceblue;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 5px 5px 8px #ccc;
  .login-header {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    box-shadow: 5px 5px 8px #ccc;
    background-color: white;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  .login-form {
    position: absolute;
    bottom: 50px;
    padding: 0 20px;
    width: 100%;
    box-sizing: border-box;
    .btns {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
